package com.iplatform.file.function.resourcedata.service.impl;



import com.iplatform.file.function.front.vo.TreeSelectVo;
import com.iplatform.file.function.resourcedata.dao.MenuDao;
import com.iplatform.file.function.resourcedata.entity.MenuBean;
import com.iplatform.file.function.resourcedata.service.MenuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @className: MenuServiceImpl
 * @description: TODO 类描述
 * @author: chensk
 * @date: 2020/12/3
 **/
@Service
public class MenuServiceImpl implements MenuService {
	
	@Resource
	private MenuDao menuDao;
	
	@Override
	public List<TreeSelectVo> selectAllMenu() {
		
		List<MenuBean> list = menuDao.selectAllMenu();
		// 返回结果集
		List<TreeSelectVo> resultList = new ArrayList<>();
		List<TreeSelectVo> treeSelectVoList = new ArrayList<>();
		// 保存所有Id
		List<String> tempList = new ArrayList<String>();
		list.forEach(a -> {
			TreeSelectVo vo = new TreeSelectVo();
			vo.setId(a.getId().toString());
			vo.setParId(a.getParentId().toString());
			vo.setLabel(a.getMenuName());
			
			tempList.add(a.getId().toString());
			treeSelectVoList.add(vo);
		});
		// 遍历
		for (TreeSelectVo iterator : treeSelectVoList) {
			
			if ("0".equals(iterator.getParId())) {
				recursionFn(treeSelectVoList, iterator);
				resultList.add(iterator);
			}
		}
		
		
		if (resultList.isEmpty()) {
			resultList = treeSelectVoList;
		}
		return resultList;
	}
	
	@Override
	public List<TreeSelectVo> selectChildMenuByMenuId(Long menuId) {
		List<MenuBean> allList = menuDao.selectAllMenu();
		
		
		List<MenuBean> childList = allList.stream().filter(e->e.getParentId().equals(menuId)).collect(Collectors.toList());
		
		List<TreeSelectVo> treeSelectVoList = new ArrayList<>();
		List<TreeSelectVo> allTreeSelectVoList = new ArrayList<>();
		List<TreeSelectVo> resultList = new ArrayList<>();
		allList.forEach(a -> {
			TreeSelectVo vo = new TreeSelectVo();
			vo.setId(a.getId().toString());
			vo.setParId(a.getParentId().toString());
			vo.setLabel(a.getMenuName());
			allTreeSelectVoList.add(vo);
		});
		childList.forEach(a -> {
			TreeSelectVo vo = new TreeSelectVo();
			vo.setId(a.getId().toString());
			vo.setParId(a.getParentId().toString());
			vo.setLabel(a.getMenuName());
			treeSelectVoList.add(vo);
		});
		
		for (TreeSelectVo iterator : treeSelectVoList) {
			recursionFn(allTreeSelectVoList, iterator);
			resultList.add(iterator);
		}
		return resultList;
	}
	
	
	private void recursionFn(List<TreeSelectVo> list, TreeSelectVo t) {
		// 得到子节点列表
		List<TreeSelectVo> childList = getChildList(list, t);
		t.setChildren(childList);
		for (TreeSelectVo tChild : childList) {
			// 判断是否有子节点
			if (hasChild(list, tChild)) {
				recursionFn(list, tChild);
			}
		}
	}
	
	/**
	 * 判断是否有子节点
	 *
	 * @param list
	 * @param tChild
	 * @return
	 */
	private boolean hasChild(List<TreeSelectVo> list, TreeSelectVo tChild) {
		return getChildList(list, tChild).size() > 0;
	}
	
	
	/**
	 * 获取子节点集合
	 *
	 * @param list
	 * @param t
	 * @return
	 */
	private List<TreeSelectVo> getChildList(List<TreeSelectVo> list, TreeSelectVo t) {
		List<TreeSelectVo> tlist = new ArrayList<TreeSelectVo>();
		Iterator<TreeSelectVo> it = list.iterator();
		while (it.hasNext()) {
			TreeSelectVo n = it.next();
			if (n.getParId() != null && n.getParId().equals(t.getId())) {
				tlist.add(n);
			}
		}
		return tlist;
	}
	
	
	@Override
	public List<MenuBean> selectChildByParentId(Long parentId, String menuName) {
		Map<String, Object> map = new HashMap<>(16);
		map.put("parentId", parentId);
		map.put("menuName", menuName);
		
		return menuDao.selectChildByParentId(map);
	}
	
	
	@Override
	public List<MenuBean> insertOrUpdate(MenuBean menuBean) {
		if (menuBean.getId() == null) {
			if (menuBean.getParentId() == null) {
				menuBean.setParentId(0L);
			}
			//验证目录是否重复
			Long parentId = menuBean.getParentId();
			String menuName = menuBean.getMenuName();
			Map<String, Object> map = new HashMap<>(16);
			map.put("parentId", parentId);
			map.put("menuName", menuName);
			List ss = menuDao.selectChildByParentId(map);
			if (ss.size() < 1) {
				menuDao.insert(menuBean);
				
			}
			return menuDao.selectById(menuBean.getId());
		} else {
			menuDao.update(menuBean);
			return menuDao.selectById(menuBean.getId());
		}
	}
	
	@Override
	public int deleteById(Long id) {
		return menuDao.deleteById(id);
	}
	
	@Override
	public MenuBean getChildIdOrCreateMenuByName(Long parentId, String menuName) {
		Map<String, Object> map = new HashMap<>();
		map.put("parentId", parentId);
		map.put("menuName", menuName);
		MenuBean menuBean = menuDao.selectChildByParentIdAndName(map);
		if (menuBean == null) {
			menuBean = new MenuBean();
			menuBean.setParentId(parentId);
			menuBean.setMenuName(menuName);
			menuDao.insert(menuBean);
		}
		return menuBean;
	}
	
	@Override
	public List<MenuBean> selectById(Long id) {
		return menuDao.selectById(id);
	}
	
	@Override
	public MenuBean selectByMenuId(Long id) {
		return menuDao.selectByMenuId(id);
	}
}
